//
//  WSTaskCell.m
//  CharonTVProject
//
//  Created by karong on 2023/7/31.
//

#import "WSTaskCell.h"
#import "WSTaskItemCell.h"
#import "WSTask.h"

@interface WSTaskCell () <WSTaskcharonTV_ItemCellDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *bgImage;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet UIView *gradientView;
@property (nonatomic, strong) NSArray * dataArray;
@end

@implementation WSTaskCell

- (void)awakeFromNib {
    [super awakeFromNib];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setViewGradientLayer];
    });
    self.bgView.layer.cornerRadius = 16;
    self.bgView.layer.borderWidth = 1;
    self.bgView.layer.borderColor = [UIColor colorFromHexRGB:@"FFFFFF"].CGColor;
    self.bgView.layer.shadowColor = [UIColor colorSameWithRGB:@"333333"].CGColor;
    self.bgView.layer.shadowRadius = 6;
    self.bgView.layer.shadowOpacity = 1;
    self.bgImage.layer.cornerRadius = 16;
    self.bgImage.layer.borderWidth = 1;
    self.bgImage.layer.borderColor = [UIColor colorFromHexRGB:@"FFFFFF"].CGColor;

}

- (void)setViewGradientLayer {
    CGRect frame = self.gradientView.frame;
    frame.size.width = WSScreenWidth - 16 - 16;
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = frame;
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:219/255.0 green:242/255.0 blue:225/255.0 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:224/255.0 green:237/255.0 blue:227/255.0 alpha:0.0].CGColor];
    gl.locations = @[@(0.0),@(1.0f)];
    [self.gradientView.layer addSublayer:gl];
}

- (void)setDataWithObject:(id)object type:(NSInteger)type {
    self.titleLabel.text = type?APPLocalized(@"新手任務"):APPLocalized(@"每日任務");
    self.dataArray = object;
    [self.listView reloadData];
}

#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSTaskItemCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSTaskItemCell" forIndexPath:indexPath];
    WSTask * task = self.dataArray[indexPath.row];
    cell.delegate = self;
    [cell setDataWithObject:task];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = WSScreenWidth - 12 - 12 - 12 - 12;
    size.height = 46;
    
    return size;
}

#pragma mark WSTaskItemCellDelegate
- (void)taskcharonTV_ItemCellAction:(UICollectionViewCell *)cell type:(NSInteger)type {
    NSIndexPath * indexPath = [self.listView indexPathForCell:cell];
    WSTask * task = self.dataArray[indexPath.row];
    if (task.charonTV_task_status == 0) {
        //跳轉
        if ([self.delegate respondsToSelector:@selector(taskcharonTV_CellTurnActionWithType:)]) {
            [self.delegate taskcharonTV_CellTurnActionWithType:type];
        }
    }
    else if (task.charonTV_task_status == 1) {
        //領取
        if ([self.delegate respondsToSelector:@selector(taskcharonTV_CellReceiveActionWithObject:)]) {
            [self.delegate taskcharonTV_CellReceiveActionWithObject:task];
        }
    }
}

@end
